package tool

import (
	"golang.org/x/image/font"
	"golang.org/x/image/font/opentype"
	"log"
	"os"
)

var (
	fontCache = make(map[int]font.Face)
	tt        *opentype.Font
)

func InitFont() {
	data, err := os.ReadFile("assets/font/ZiKuXingQiuFeiYangTi-2.ttf")
	if err != nil {
		log.Fatal("字体读取失败:", err)
	}
	tt, err = opentype.Parse(data)
	if err != nil {
		log.Fatal("字体解析失败:", err)
	}
}
func GetFont(size int) font.Face {
	if face, ok := fontCache[size]; ok {
		return face
	}
	const dpi = 144
	face, err := opentype.NewFace(tt, &opentype.FaceOptions{
		Size:    float64(size),
		DPI:     dpi,
		Hinting: font.HintingFull,
	})
	if err != nil {
		log.Fatal("字体加载失败:", err)
	}
	fontCache[size] = face
	return face
}
